package com.ruoyi.sms.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sms.domain.Channel;
import com.ruoyi.sms.service.IChannelService;
import com.ruoyi.sms.service.IServiceTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sms.domain.Cmd;
import com.ruoyi.sms.service.ICmdService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 指令列表Controller
 *
 * @author wukong
 * @date 2021-03-30
 */
@RestController
@RequestMapping("/sms/cmd")
public class CmdController extends BaseController {
    @Autowired
    private ICmdService cmdService;

    @Autowired
    private IChannelService channelService;

    @Autowired
    private IServiceTypeService serviceTypeService;

    /**
     * 查询指令列表列表
     */
    @PreAuthorize("@ss.hasPermi('sms:cmd:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cmd cmd) {
        startPage();
        List<Cmd> list = cmdService.selectCmdList(cmd);
        return getDataTable(list);
    }

    /**
     * 导出指令列表列表
     */
    @PreAuthorize("@ss.hasPermi('sms:cmd:export')")
    @Log(title = "指令列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Cmd cmd) {
        List<Cmd> list = cmdService.selectCmdList(cmd);
        ExcelUtil<Cmd> util = new ExcelUtil<Cmd>(Cmd.class);
        return util.exportExcel(list, "cmd");
    }

    /**
     * 获取指令列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('sms:cmd:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        AjaxResult ajax = AjaxResult.success();
        List<Channel> channels = channelService.selectChannelAll();
        ajax.put("channels", channels);
        ajax.put("serviceTypes", serviceTypeService.selectServiceTypeAll());
        if (StringUtils.isNotNull(id) && id!=-1) {
            ajax.put(AjaxResult.DATA_TAG, cmdService.selectCmdById(id));
            //ajax.put("channelIds", channelService.selectListByCmdId(id));
            //ajax.put("serviceTypeIds", serviceTypeService.selectListByCmdId(id));
        }
        return ajax;
        //return AjaxResult.success(cmdService.selectCmdById(id));
    }

    /**
     * 新增指令列表
     */
    @PreAuthorize("@ss.hasPermi('sms:cmd:add')")
    @Log(title = "指令列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cmd cmd) {
        return toAjax(cmdService.insertCmd(cmd));
    }

    /**
     * 修改指令列表
     */
    @PreAuthorize("@ss.hasPermi('sms:cmd:edit')")
    @Log(title = "指令列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cmd cmd) {
        return toAjax(cmdService.updateCmd(cmd));
    }

    /**
     * 删除指令列表
     */
    @PreAuthorize("@ss.hasPermi('sms:cmd:remove')")
    @Log(title = "指令列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmdService.deleteCmdByIds(ids));
    }
}
